function HandleBModel(gui, user, date, options, data) {
  this._gui = gui;
  this._user = user;
  this._date = date;
  this._options = options;
  this._data = data;
  
  this._popupBox = new ModulePopupBox();
  
  this._createElements();
};

HandleBModel.prototype._createElements = function() {
  var _self = this, tr, td;
  DOMUtils.removeTableRows(this._gui.states, 1);
  DOMUtils.removeTableRows(this._gui.transitions, 1);

  this._states = [];
  this._transitions = [];
  if (this._data['0'] && this._data['0'].data.length != 0) {
    this._model = this._data['0'].data[0];
    this._states = this._data['1'].data;
    this._transitions = this._data['2'].data;
  } else {
    this._model = pz_model.instance();
  }
  
  this._gui.name.value = this._model.name;
  this._gui.name.onchange = function() { _self._model.name = this.value; };
  
  this._gui.update.onclick = function() { new RequestUtils()._write('pz_model', [_self._model], [], function() { location.reload(); }, {pos: DOMUtils.findPos(this)}); };
  
  if (this._states.length == 0) {
    tr = this._gui.states.insertRow(-1);
    td = tr.insertCell(-1);
    td.colSpan = 3;
    td.style.height = '23px';
    td.style.textAlign = 'left';
    td.style.paddingLeft = '5px';
    td.appendChild(document.createTextNode('n/a'));
  }
  for (var i = 0, il = this._states.length; i < il;i ++) {
    var state = this._states[i];
    tr = this._gui.states.insertRow(-1);
    tr.style.backgroundColor = (tr.rowIndex % 2 == 0) ? '#ececec' : '#fff';
    td = tr.insertCell(-1);
    td.style.height = '23px';
    td.style.padding = '0 5px';
    td.appendChild(document.createTextNode(parseInt(i+1)));
    td = tr.insertCell(-1);
    td.style.textAlign = 'left';
    td.style.padding = '0 5px';
    td.appendChild(document.createTextNode(state.name));
    td = tr.insertCell(-1);
    td.style.padding = '0 5px';
    var modify = document.createElement('img');
    modify.src = 'image/edit.png';
    modify.style.cursor = 'pointer';
    modify._state = state;
    modify.onclick = function() { var pos = DOMUtils.findPos(this);
                                  var div = document.createElement('div');
                                  MiscUtils.dialog(_self._popupBox, pos, div, null, null, {t: this._state.name, width: 500, height: 50});
                                  new ModuleDialogAddState(document, div, 300, 30, _self._user, _self._date, { object: this._state, popupBox: _self._popupBox, callbackFunc: function() { location.reload(); } });
                                  return false;
                                };
    td.appendChild(modify);
    var remove = document.createElement('img');
    remove.src = 'image/delete.png';
    remove.style.margin = '0 5px';
    remove.style.cursor = 'pointer';
    remove._state = state;
    remove.onclick = function() { new RequestUtils()._write('pz_model_state', [], [this._state], function() { location.reload(); }, {pos: DOMUtils.findPos(this)}); 
    };
    td.appendChild(remove);
  }
  
  this._gui.addState.onclick = function() { var pos = DOMUtils.findPos(this);
                                            var div = document.createElement('div');
                                            var state = pz_model_state.instance();
                                            state.model_id = _self._model.id;
                                            MiscUtils.dialog(_self._popupBox, pos, div, null, null, {t: 'Add State', width: 500, height: 50});
                                            new ModuleDialogAddState(document, div, 300, 30, _self._user, _self._date, { object: state, popupBox: _self._popupBox, callbackFunc: function() { location.reload(); } });
                                            return false;
                                          };

                                          
  if (this._transitions.length == 0) {
    tr = this._gui.transitions.insertRow(-1);
    td = tr.insertCell(-1);
    td.colSpan = 7;
    td.style.height = '23px';
    td.style.textAlign = 'left';
    td.style.paddingLeft = '5px';
    td.appendChild(document.createTextNode('n/a'));
  }
  for (var i = 0, il = this._transitions.length; i < il;i ++) {
    var tran = this._transitions[i];
    tr = this._gui.transitions.insertRow(-1);
    tr.style.backgroundColor = (tr.rowIndex % 2 == 0) ? '#ececec' : '#fff';
    td = tr.insertCell(-1);
    td.style.height = '23px';
    td.style.padding = '0 5px';
    td.appendChild(document.createTextNode(parseInt(i+1)));
    td = tr.insertCell(-1);
    td.style.textAlign = 'left';
    td.style.padding = '0 5px';
    td.appendChild(document.createTextNode(tran.name));
    td = tr.insertCell(-1);
    td.style.textAlign = 'left';
    td.style.padding = '0 5px';
    td.appendChild(document.createTextNode(tran.user_type));
    td = tr.insertCell(-1);
    td.style.textAlign = 'left';
    td.style.padding = '0 5px';
    td.appendChild(document.createTextNode((PPublishUtils.object(tran.source_id, this._states)) ? PPublishUtils.object(tran.source_id, this._states).name : ''));
    td = tr.insertCell(-1);
    td.style.textAlign = 'left';
    td.style.padding = '0 5px';
    td.appendChild(document.createTextNode((PPublishUtils.object(tran.target_id, this._states)) ? PPublishUtils.object(tran.target_id, this._states).name : ''));
    td = tr.insertCell(-1);
    td.style.textAlign = 'left';
    td.style.padding = '0 5px';
    td.appendChild(document.createTextNode(tran.day));
    td = tr.insertCell(-1);
    td.style.padding = '0 5px';
    var modify = document.createElement('img');
    modify.src = 'image/edit.png';
    modify.style.cursor = 'pointer';
    modify._tran = tran;
    modify.onclick = function() { var pos = DOMUtils.findPos(this);
                                  var div = document.createElement('div');
                                  MiscUtils.dialog(_self._popupBox, pos, div, null, null, {t: this._tran.name, width: 500, height: 50});
                                  new ModuleDialogAddTransition(document, div, 300, 30, _self._user, _self._date, { object: this._tran, states: _self._states, popupBox: _self._popupBox, callbackFunc: function() { location.reload(); } });
                                  return false;
                                };
    td.appendChild(modify);
    var remove = document.createElement('img');
    remove.src = 'image/delete.png';
    remove.style.margin = '0 5px';
    remove.style.cursor = 'pointer';
    remove._tran = tran;
    remove.onclick = function() { new RequestUtils()._write('pz_model_transition', [], [this._tran], function() { location.reload(); }, {pos: DOMUtils.findPos(this)}); 
                                };
    td.appendChild(remove);
  }
  
  
  this._gui.addTransition.onclick = function() { var pos = DOMUtils.findPos(this);
                                                 var div = document.createElement('div');
                                                 var tran = pz_model_transition.instance();
                                                 tran.model_id = _self._model.id;
                                                 MiscUtils.dialog(_self._popupBox, pos, div, null, null, {t: 'Add Transition', width: 500, height: 50});
                                                 new ModuleDialogAddTransition(document, div, 300, 30, _self._user, _self._date, { object: tran, states: _self._states, popupBox: _self._popupBox, callbackFunc: function() { location.reload(); } });
                                                 return false;
                                               };
};